package com.tozed.test;

import java.util.ArrayList;

import com.tozed.widget.TitleBar;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class LcdTestActivity extends Base implements OnClickListener, Runnable
{
	private final static int MSG_RED = 1;
	private final static int MSG_GREEN = 2;
	private final static int MSG_BLUE = 3;
	private final static int MSG_HORIZONTAL_STRIPES = 4;
	private final static int MSG_VERTICAL_STRIPES = 5;
	private final static int MSG_SHOW_TITLE = 6;
	private final static int MSG_SHOW_BUTTOM = 7;
	private final static int MSG_AUTO_TEST_SUCC = 8;
	private final static int MSG_AUTO_TEST_FAIL = 9;
	
	private TitleBar mTitleBar;
	private Button mBtnPass, mBtnFail;
	private RelativeLayout mFullScreen;
	private LinearLayout mButtomBar;
	private LinearLayout mHorizontalStripes;
	private LinearLayout mVerticalStripes;
	
	private ArrayList<TextView> mTvList = new ArrayList<TextView>();

	private int mTestType = Constant.TYPE_TEST_SINGLE;
	
	private Handler mHandler = new Handler() {
		
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case MSG_RED:
				mFullScreen.setBackgroundColor(0xFFFF0000);
				break;
			case MSG_GREEN:
				mFullScreen.setBackgroundColor(0xFF00FF00);
				break;
			case MSG_BLUE:
				mFullScreen.setBackgroundColor(0xFF0000FF);
				break;
			case MSG_SHOW_TITLE:
				getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
				mTitleBar.setVisibility(View.VISIBLE);
				break;
			case MSG_SHOW_BUTTOM:
				mButtomBar.setVisibility(View.VISIBLE);
				break;
			case MSG_HORIZONTAL_STRIPES:
				addHorizontalTextView();
				showStripes();
				mHorizontalStripes.setVisibility(View.VISIBLE);
				break;
			case MSG_VERTICAL_STRIPES:
				addVerticalTextView();
				showStripes();
				mHorizontalStripes.setVisibility(View.GONE);
				mVerticalStripes.setVisibility(View.VISIBLE);
				break;
			case MSG_AUTO_TEST_SUCC:
				mBtnPass.performClick();
				/*new Handler().postDelayed(new Runnable(){	 
					public void run() {    
					//execute the task	 
					mBtnPass.performClick();
					}	 
				 }, 1000);*/ 
				break;
			case MSG_AUTO_TEST_FAIL:
				mBtnFail.performClick();
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.lcd_test);

		Intent intent = getIntent();
		if (intent != null)
			mTestType = intent.getIntExtra("TEST_TYPE", Constant.TYPE_TEST_SINGLE);
		
		mTitleBar = (TitleBar)findViewById(R.id.title_bar);
		mTitleBar.setTiteText(R.string.str_test_lcd);
		mTitleBar.hideRightButton();
		mTitleBar.setVisibility(View.GONE);
				
		mBtnPass = (Button)findViewById(R.id.btn_pass);
		mBtnFail = (Button)findViewById(R.id.btn_fail);
		mBtnPass.setOnClickListener(this);
		mBtnFail.setOnClickListener(this);
		
		mFullScreen = (RelativeLayout)findViewById(R.id.rl_color);
		mFullScreen.setBackgroundColor(0xFFFF0000);
		
		mButtomBar = (LinearLayout)findViewById(R.id.layout_bottom_bar);
		mButtomBar.setVisibility(View.GONE);
		
		mHorizontalStripes = (LinearLayout)findViewById(R.id.ll_horizontal_stripes);
		mHorizontalStripes.setVisibility(View.GONE);
		
		mVerticalStripes = (LinearLayout)findViewById(R.id.ll_vertical_stripes);
		mVerticalStripes.setVisibility(View.GONE);
		
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		new Thread(this).start();
	}
	
	public void addHorizontalTextView() {
		TextView tv;
		LayoutParams params = new LayoutParams(
				LayoutParams.MATCH_PARENT, 0, 1);
		
		mTvList.clear();	
		for (int i = 0; i < 20; i++) {
			tv = new TextView(this);
			mHorizontalStripes.addView(tv, params);
			mTvList.add(tv);
		}
	}
	
	public void addVerticalTextView() {
		TextView tv;
		LayoutParams params = new LayoutParams(
				0, LayoutParams.MATCH_PARENT, 1);
		
		mTvList.clear();	
		for (int i = 0; i < 12; i++) {
			tv = new TextView(this);
			mVerticalStripes.addView(tv, params);
			mTvList.add(tv);
		}
	}
	
	public void showStripes() {		
		for (int i = 0; i < mTvList.size(); i++) {
			if (i%2 != 0) {
				mTvList.get(i).setBackgroundColor(0xFFFFFFFF);
			} else {
				mTvList.get(i).setBackgroundColor(0xFF000000);
			}
		}
	}
	
	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.btn_pass:
			Intent intent = new Intent();
			intent.putExtra("KEY_TEST_STATUS", 1);
			setResult(RESULT_OK, intent);
			finish();
			break;
			
		case R.id.btn_fail:
			Intent intent2 = new Intent();
			intent2.putExtra("KEY_TEST_STATUS", 0);
			setResult(RESULT_OK, intent2);
			finish();
			break;
			
		default:
			break;
		}
	}

	@Override
	public void run() {
		try {
			Thread.sleep(1000);
			mHandler.sendEmptyMessage(MSG_GREEN);
			Thread.sleep(1000);
			mHandler.sendEmptyMessage(MSG_BLUE);
			Thread.sleep(1000);
			mHandler.sendEmptyMessage(MSG_HORIZONTAL_STRIPES);
			Thread.sleep(2000);
			mHandler.sendEmptyMessage(MSG_VERTICAL_STRIPES);
			Thread.sleep(2000);
			mHandler.sendEmptyMessage(MSG_SHOW_TITLE);
			mHandler.sendEmptyMessage(MSG_SHOW_BUTTOM);

			if (mTestType == Constant.TYPE_TEST_AGING)
				mHandler.sendEmptyMessage(MSG_AUTO_TEST_SUCC);

		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
	}
}
